import { getUrlByQuery, convertValues } from '../utils';


export interface IRouteProps {
    /** 跳转路径 */
    url: string;
    /** 页面传参 */
    query?: object;
    /** 页面跳转动画 */
    animationType?:
    | "auto"
    | "none"
    | "slide-in-right"
    | "slide-in-left"
    | "slide-in-top"
    | "slide-in-bottom"
    | "fade-in"
    | "zoom-out"
    | "zoom-fade-out"
    | "pop-in";
    /** 页面跳转动画时间 */
    animationDuration?: number;
    /** 页面间通信接口 */
    events?: object;
}


/** 页面跳转 */
const navigateTo = (options: IRouteProps) => {
    let {
        url,
        query = {},
        animationType = "pop-in",
        animationDuration = 300,
        events = null
    } = options;
    query = convertValues(query);
    url = getUrlByQuery(url, query);
    uni.navigateTo({ url, animationType, animationDuration, events });
};

/**
 * 页面回退
 * @param option 参数
 * @param jumpStemp 回退页数
 */
const navigateBack = (option: UniApp.NavigateBackOptions, jumpStemp = 1) => {
    uni.navigateBack({ ...option, delta: jumpStemp });
};


/** 页面重定向 */
const redirectTo = (options: IRouteProps) => {
    let {
        url,
        query = {},
        animationType = "pop-in",
        animationDuration = 300,
        events = null
    } = options;
    query = convertValues(query);
    url = getUrlByQuery(url, query);
    uni.navigateTo({ url, animationType, animationDuration, events });
};


/** 跳转tabbar页面,url不能带参数 */
const goTabBar = (option: UniApp.SwitchTabOptions) => {
    uni.switchTab(option);
};

/** 关闭全部页面重定向到特定页面 */
const reLaunch = (options: IRouteProps) => {
    let { url, query = {} } = options;
    url = getUrlByQuery(url, query);
    uni.reLaunch({ url });
};


export default {
    navigateTo,
    navigateBack,
    redirectTo,
    goTabBar,
    reLaunch
}